package game.util;

import game.Constants;
import game.Message;
import game.Person;
import game.communication.Communication;
import game.communication.GreetingCommunication;
import game.communication.InterestCommunication;
import game.manager.RelationshipManager;
import game.manager.RelationshipManager.Relationship;
import game.util.InterestUtil.Interest;
import game.util.TraitUtil.Trait;

import java.util.Collection;
import java.util.List;
import java.util.Vector;

public class CommunicationUtil {

	public Collection<Communication> getCommunications(Person sender,
			Person receiver, List<Message> messages) {
		final Collection<Communication> communications = new Vector<Communication>();

		if (messages.isEmpty()) {
			// Get greetings
			List<Communication> traitTopics = this.getGreetings(sender,
					receiver);
			if (traitTopics != null) {
				communications.addAll(traitTopics);
			}
		} else {
			final Relationship relationship = RelationshipManager.getManager()
					.getRelationship(sender, receiver);
			final int length = relationship.getLength();
			final int strength = relationship.getStrength();

			// Get conversations about interests
			for (Interest interest : sender.getInterests()) {
				List<Communication> interestTopics = this
						.getCommunications(interest);
				if (interestTopics != null) {
					communications.addAll(interestTopics);
				}
			}

			// Get conversations about appearance
			final int attraction = RelationshipManager.getManager()
					.getLooksCompatibility(sender, receiver);
			for (Trait trait : sender.getTraits()) {
				List<Communication> traitTopics = this.getCommunications(trait,
						attraction);

				if (traitTopics != null) {
					communications.addAll(traitTopics);
				}
			}
		}

		return communications;
	}

	private List<Communication> getCommunications(Trait trait, int attraction) {
		final List<Communication> conversations = new Vector<Communication>();

		if (attraction >= Constants.ATTRACTION_LOOKS_HIGH) {
			// TODO
		} else if (attraction >= Constants.ATTRACTION_LOOKS_MEDIUM) {
			// TODO
		} else if (attraction >= Constants.ATTRACTION_LOOKS_LOW) {
			// TODO
		}
		return conversations;
	}

	private List<Communication> getCommunications(Interest interest) {
		final List<Communication> conversations = new Vector<Communication>();
		final List<String> conversationText = interest.getCommunications();
		for (String conversation : conversationText) {
			InterestCommunication conversationCommunication = new InterestCommunication(
					conversation);
			conversations.add(conversationCommunication);
		}
		return conversations;
	}

	private List<Communication> getGreetings(final Person sender,
			final Person receiver) {
		final List<Communication> greetings = new Vector<Communication>();
		final List<Trait> senderTraits = sender.getTraits();
		for (Trait senderTrait : senderTraits) {
			List<String> greetingsText = senderTrait.getGreetings();
			for (String greetingText : greetingsText) {
				greetings
						.add(new GreetingCommunication(greetingText, receiver));
			}
		}
		return greetings;
	}
}
